package cz.muni.fi.pa165.travelagency.services;

import cz.muni.fi.pa165.travelagency.api.entities.ICustomer;
import cz.muni.fi.pa165.travelagency.api.services.ICustomerService;
import cz.muni.fi.pa165.travelagency.dto.CustomerDTO;
import cz.muni.fi.pa165.travelagency.entities.Customer;
import cz.muni.fi.pa165.travelagency.interceptors.ShiroSecured;
import cz.muni.fi.pa165.travelagency.persistence.ICustomerDAO;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;

/**
 *
 * @author Jan Novotný
 */
@Stateless(name = "customerService", mappedName = "customerService")
@Local(ICustomerService.class)
@TransactionManagement
@ShiroSecured
public class CustomerService implements ICustomerService {

    private Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
    
    @EJB
    private ICustomerDAO customerDAO;
    
    void setCustomerDao(ICustomerDAO customerDAO)
    {
        this.customerDAO = customerDAO;
    }

    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("customer:create")    
    public void createCustomer(ICustomer customer) {
        Customer c = mapper.map(customer, Customer.class);
        customerDAO.createCustomer(c);
        customer.setId(c.getId());
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("customer:delete")
    public void deleteCustomer(ICustomer customer) {
        customerDAO.deleteCustomer(mapper.map(customer, Customer.class));
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("customer:find")
    public List<ICustomer> findAll() {
        List<Customer> customers = customerDAO.findAll();
        List<ICustomer> result = new ArrayList<>();
        for (Customer c : customers) {
            result.add(mapper.map(c, CustomerDTO.class));
        }
        return result;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("customer:find")
    public List<ICustomer> findByName(String name) {
        List<Customer> customers = customerDAO.findByName(name);
        List<ICustomer> result = new ArrayList<>();
        for (Customer c : customers) {
            result.add(mapper.map(c, CustomerDTO.class));
        }
        return result;
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("customer:find")
    public ICustomer getCustomer(Long id) {
        Customer found = customerDAO.getCustomer(id);
        return found == null ? null : mapper.map(found, CustomerDTO.class);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @RequiresPermissions("customer:update")
    public void updateCustomer(ICustomer customer) {
        customerDAO.updateCustomer(mapper.map(customer, Customer.class));
    }
}
